package JdbcTemplate.operation.query;

import JdbcTemplate.config.Config;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcOperations;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

public class Query2 {

    private static Person person;

    public Person findOnre(int id){
        String query="SELECT * FROM spitter WHERE id_identity=?;";
         ApplicationContext context=new AnnotationConfigApplicationContext(Config.class);
        JdbcOperations jdbcOperations= (JdbcOperations)context.getBean("jdbcOperations");

        //使用预处理语句
        return jdbcOperations.queryForObject(query,this::mapPerson,id);//this代表该类对象
        //使用常规语句
//        return jdbcOperations.queryForObject(query,this::mapPerson);
    }

    public  Person mapPerson(ResultSet rs,int row)throws SQLException{
        return person=new Person(
                rs.getInt("id_identity"),
                rs.getString("username"),
                rs.getString("password"),
                rs.getString("first_name"),
                rs.getString("last_name"),
                rs.getString("email"));
    }

    public static void main(String[] args) {

        Query2 query2=new Query2();
        query2.findOnre(9);
        System.out.println(person.getEmail());

    }

}
